﻿using ViewMaker.Core.ViewModels;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using ViewMaker.Core;
using System.Xml;
using System.Windows.Controls;

namespace ViewMaker.Core.Tests
{
    [TestClass()]
    public class ViewLayoutViewModelTest
    {
        class TestContentView : IViewContent
        {
            public object Content
            {
                get { return null; }
            }

        }

        [TestMethod()]
        public void 簡単なモデルを編集()
        {
            var vm = new TestClass();
            var view = new TestContentView();
            var item = new ViewLayoutItem(null, "Test", ViewControlType.StackPanel, ".", false, "テスト");
            var target = new ViewLayoutViewModel(item, typeof(TestClass));

            target.GenerateXamlCode();
            var step1 = TestHelper.GetFrameworkElement<ContentControl>(target.XamlCode);
            Assert.AreEqual(0, ((StackPanel)step1.Content).Children.Count);

            //テキストボックスを追加
            target.CurrentItem = target.Root[0];
            target.AddControlCommand.Execute(ViewControlType.TextBox);
            target.CurrentItem.Name = "textBox1";

            target.GenerateXamlCode();
            var step2 = TestHelper.GetFrameworkElement<ContentControl>(target.XamlCode);
            Assert.AreEqual(1, ((StackPanel)step2.Content).Children.Count);
            Assert.AreEqual("textBox1", ((TextBox)((Panel)((Panel)step2.Content).Children[0]).Children[1]).Name);

            //Outputフィールドの追加
            target.CurrentItem = target.Root[0];
            target.GenerateItemCommand.Execute("Output");
            target.GenerateXamlCode();
            var step3 = TestHelper.GetFrameworkElement<ContentControl>(target.XamlCode);
            Assert.AreEqual(2, ((StackPanel)step3.Content).Children.Count);
            Assert.AreEqual("Output", ((TextBox)((Panel)((Panel)step3.Content).Children[1]).Children[1]).Name);

            //Outputフィールドを上に
            target.CurrentItem = target.Root[0].Children[1];
            target.UpCommand.Execute(null);
            target.GenerateXamlCode();
            var step4 = TestHelper.GetFrameworkElement<ContentControl>(target.XamlCode);
            Assert.AreEqual("Output", ((TextBox)((Panel)((Panel)step4.Content).Children[0]).Children[1]).Name);

            //Outputフィールドを下に
            target.DownCommand.Execute(null);
            target.GenerateXamlCode();
            var step5 = TestHelper.GetFrameworkElement<ContentControl>(target.XamlCode);
            Assert.AreEqual("Output", ((TextBox)((Panel)((Panel)step5.Content).Children[1]).Children[1]).Name);

            //textBox1を削除
            target.CurrentItem = target.Root[0].Children[0];
            target.RemoveItemCommand.Execute(null);
            target.GenerateXamlCode();
            var step6 = TestHelper.GetFrameworkElement<ContentControl>(target.XamlCode);
            Assert.AreEqual(1, ((StackPanel)step6.Content).Children.Count);
            Assert.AreEqual("Output", ((TextBox)((Panel)((Panel)step6.Content).Children[0]).Children[1]).Name);

            target.SelectByName("Test");
            target.ModifyControlCommand.Execute(ViewControlType.Canvas);
            target.GenerateXamlCode();
            var step7 = TestHelper.GetFrameworkElement<ContentControl>(target.XamlCode);
            Assert.IsInstanceOfType(step7.Content,typeof(Canvas));

        }

        [TestMethod()]
        public void 左右への移動編集()
        {
            var vm = new TestClass();
            var view = new TestContentView();
            var item = new ViewLayoutItem(null, "Test", ViewControlType.StackPanel, ".", false, "テスト");
            var target = new ViewLayoutViewModel(item, typeof(TestClass));

            target.CurrentItem = target.Root[0];
            target.AddControlCommand.Execute(ViewControlType.StackPanel);
            target.CurrentItem = target.Root[0];
            target.GenerateItemCommand.Execute("Input");

            target.SelectByName("Input");
            target.RightCommand.Execute(null);

            target.GenerateXamlCode();
            var right = TestHelper.GetFrameworkElement<ContentControl>(target.XamlCode);
            Assert.AreEqual("Input", ((TextBox)((Panel)((Panel)((Panel)right.Content).Children[0]).Children[0]).Children[1]).Name);

            target.LeftCommand.Execute(null);
            target.GenerateXamlCode();
            var left = TestHelper.GetFrameworkElement<ContentControl>(target.XamlCode);
            Assert.AreEqual("Input", ((TextBox)((Panel)((Panel)left.Content).Children[1]).Children[1]).Name);
        }

        [TestMethod()]
        public void カレント編集()
        {
            var vm = new TestClass();
            var view = new TestContentView();
            var item = new ViewLayoutItem(null, "Test", ViewControlType.StackPanel, ".", false, "テスト");
            var target = new ViewLayoutViewModel(item, typeof(TestClass));

            target.CurrentItem = target.Root[0];
            target.AddControlCommand.Execute(ViewControlType.StackPanel);
            target.CurrentItem = target.Root[0];
            target.GenerateItemCommand.Execute("Input");
            target.GenerateItemCommand.Execute("Output");

            target.SelectByName("Input");

            Assert.AreEqual("Input", target.CurrentItem.Name);
        }

        [TestMethod()]
        public void Viewコマンド()
        {
            var vm = new TestClass();
            var view = new TestContentView();
            var item = new ViewLayoutItem(null, "Test", ViewControlType.StackPanel, ".", false, "テスト");
            var target = new ViewLayoutViewModel(item, typeof(TestClass));
            target.ViewCommandNotified += new EventHandler<ViewCommandEventArgs>(target_ViewCommandNotified);

            target.Activate();
            Assert.AreEqual("Activate", this.command);
            target.CloseCommand.Execute(null);
            Assert.AreEqual("Hide", this.command);
            target.Close();
            Assert.AreEqual("Close", this.command);
        }

        private string command;
        void target_ViewCommandNotified(object sender, ViewCommandEventArgs e)
        {
            if (e.Command == "EndCommand") return;
            this.command = e.Command;
        }

    }
}
